﻿
Imports System.Text
Imports System.IO
Imports cxyLib
Imports System.Text.RegularExpressions

Public Class Form2



    Dim _imagedownloader As ImageDownLoader
    Dim _pageLoader As GetWebPage
    Dim _changPinInfo As Hashtable '这个用来存储产品的信息,以产品的id作为主键

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Enabled = False 

        Dim _help As New ShowHelper()
        Dim _downloader As New cxyLib.HttpDownLoader(cbCoding.Text.Trim) '指定下载器及编码
        Dim res As New StringBuilder
         


        Dim imageString As New StringBuilder
        Dim titleString As New StringBuilder

        Dim taskmember As Integer = urlarray.Count
        Dim i As Integer = 1

        Dim classfihash As New Hashtable



        For Each _url As String In urlarray
            Dim onetask As New RegexTask
            onetask.WebUrl = _url
            Dim webContent As String = _downloader.HttpGet(_url, "", "")
            onetask.WebString = webContent


            Dim lastMatch As String = webContent

            Dim task As New RegexTask


            For Each entry As DictionaryEntry In Me.regHash
                Dim rege As WebPageRegex = TryCast(entry.Value, WebPageRegex)
                res.Append(rege.RegexMatchString(onetask, chkbox.Checked))
                ' imageString.Append(rege.GetNamedGroupString)


                Dim ht As Hashtable = rege.NamedGroup
                For Each mentry As DictionaryEntry In ht
                    Dim nv As NamedGroupAndValue = TryCast(mentry.Value, NamedGroupAndValue)
                    If (classfihash.ContainsKey(mentry.Key)) Then
                        classfihash(mentry.Key) += nv.MatchValue & vbCrLf
                    Else
                        classfihash.Add(mentry.Key, nv.MatchValue & vbCrLf)
                    End If
                Next



                '   imageString.Append(_help.ArrayList2String("", rege.ImageAL))
                '  titleString.Append(_help.ArrayList2String("", rege.TitleAL))
            Next

            i += 1
            lbStatus.Text = "正在处理第" + i.ToString + "个,共" + taskmember.ToString + "个"
            Application.DoEvents()
        Next


        Dim sb As New StringBuilder

        '取出分类好的
        For Each entry As DictionaryEntry In classfihash
            sb.Append(entry.Key & vbCrLf & entry.Value & vbCrLf)
        Next



        _help.SaveFileDateTimeFileName(res.ToString() & imageString.ToString() & sb.ToString() & titleString.ToString())



        '打开路径 
        Process.Start(AppDomain.CurrentDomain.BaseDirectory)
        'Shell(fApplicationPath) 'vb.net 专用 
        MessageBox.Show("处理完成")
        Button1.Enabled = True
    End Sub








    Private Sub btnAddList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddList.Click
        btnAddList.Enabled = False
        Try




            Dim myhash As New Hashtable
            Dim fileurl As String = txtFileUrl.Text.Trim
            Dim filename As String = txtFileName.Text.Trim

            If fileurl <> "" And filename <> "" Then



                Dim linearry() As String = fileurl.Split(vbCrLf)
                For Each Str As String In linearry
                    Dim tp() As String = Str.Split(",")
                    Dim tpfd As New DownFileData
                    tpfd.FileId = tp(0)
                    tpfd.FileUrl = tp(1)
                    myhash.Add(tpfd.FileId.Trim, tpfd)
                    'lbQueue.Items.Add()
                Next
                '以上解析出文件地址


                Dim l_array() As String = filename.Split(vbCrLf)

                For Each Str As String In l_array
                    Dim tp() As String = Str.Split(",")

                    Dim fileid As String = tp(0)

                    Dim tpfd As DownFileData = myhash(fileid.Trim)
                    tpfd.FileName = tp(1)

                Next
                '以上解析出文件名称
            End If


            If txtNameUrl.Text.Trim <> "" Then
                Dim full_array As String = txtNameUrl.Text.Trim.Replace(vbCrLf, "|")
                Dim f_array() As String = full_array.Split("|")
                For Each Str As String In f_array
                    Dim tp() As String = Str.Split(",")
                    Dim tpfd As New DownFileData

                    If tp(0).StartsWith("http") Then
                        tpfd.FileUrl = tp(0)
                        tpfd.FileName = tp(1)
                    Else
                        tpfd.FileName = tp(0)
                        tpfd.FileUrl = tp(1)
                    End If
                    myhash.Add(tpfd.FileUrl, tpfd)

                Next
            End If


            If txtFileNameAndUrl.Text.Trim <> "" Then

                Dim full_string As String = txtFileNameAndUrl.Text.Trim.Replace(vbCrLf, "|")
                Dim f_array() As String = full_string.Split("|")
                For Each Str As String In f_array

                    Dim tpfd As New DownFileData
                    tpfd.FileUrl = Str
                    tpfd.FileName = Str.Substring(Str.LastIndexOf("/")).Replace(" ", "") '去除文件名里的空格
                    If Not myhash.ContainsKey(tpfd.FileUrl) Then
                        myhash.Add(tpfd.FileUrl, tpfd)
                    End If


                Next

            End If
            DownLoadFile(myhash)

        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try

        MessageBox.Show("处理完成")
        btnAddList.Enabled = True

    End Sub

    Private Function DownLoadFile(ByVal taskhash As Hashtable)

        Dim _imageget As New ImageDownLoader
        For Each tpfd As DownFileData In taskhash.Values

            Try
                lbQueue.Items.Add(tpfd.FileName)
                '开始下载图像,并处理
                Application.DoEvents()
                Dim tpsavepath As String = ""
                If tpfd.FileName.Contains(".") Then
                    tpsavepath = txtSavePath.Text.Trim() + tpfd.FileName  '如果有文件后缀,就下载
                Else
                    tpsavepath = txtSavePath.Text.Trim() + tpfd.FileName + "." + GetFileExt(tpfd.FileUrl)
                End If

                _imageget.DownLoadImage(tpfd.FileUrl, tpsavepath) '下载并保存
                System.Threading.Thread.Sleep(50)
                Application.DoEvents()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try



        Next

    End Function

    Private Function DownLoadFile(ByVal taskarray As ArrayList)

        Dim _imageget As New ImageDownLoader
        For Each tpfd As DownFileData In taskarray

            Try
                lbQueue.Items.Add(tpfd.FileName)
                '开始下载图像,并处理
                Application.DoEvents()
                Dim tpsavepath As String = ""
                If tpfd.FileName.Contains(".") Then
                    tpsavepath = txtSavePath.Text.Trim() + tpfd.FileName  '如果有文件后缀,就下载
                Else
                    tpsavepath = txtSavePath.Text.Trim() + tpfd.FileName + "." + GetFileExt(tpfd.FileUrl)
                End If

                _imageget.DownLoadImage(tpfd.FileUrl, tpsavepath) '下载并保存
                System.Threading.Thread.Sleep(50)
                Application.DoEvents()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try



        Next

    End Function




    '取得文件的扩展名
    Private Function GetFileExt(ByVal filename As String) As String
        Return filename.Substring(filename.LastIndexOf(".") + 1)
    End Function


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim fldselect As New FolderBrowserDialog

        fldselect.RootFolder = Environment.SpecialFolder.MyComputer

        If fldselect.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtSavePath.Text = fldselect.SelectedPath
        End If
    End Sub

    Private regArray As ArrayList  '正则表达式列表
    Dim urlarray As New ArrayList() 'url  地址列表
    Dim regHash As New Hashtable() '正则表达式列表



    '添加正则表达式字符串
    Private Sub btnaddReg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddReg.Click
        btnaddReg.Enabled = False
        Dim tpstr As String = txtRegString.Text.Trim

        If regArray Is Nothing Then
            regArray = New ArrayList
        End If

        Dim level As Integer = Int32.Parse(txbLevel.Text)
        Dim regex As String = txtRegString.Text ' 用户输入的正则表达式


        If regHash.ContainsKey(level) Then
            Dim _regex1 As WebPageRegex
            _regex1 = TryCast(regHash(level), WebPageRegex)
            _regex1.AddRegexString(regex)

        Else

            Dim _regex As New WebPageRegex()
            _regex.Levels = Int32.Parse(txbLevel.Text)
            _regex.AddRegexString(regex)
            regHash.Add(level, _regex)
        End If

        ReFreshListBox() '刷新一下显示 




        'If regArray.Contains(tpstr) Then
        '    MessageBox.Show("正则表达式已经存在了!")
        'Else
        '    regArray.Add(tpstr)
        '    ReFreshListBox() '刷新一下显示
        'End If


        txtRegString.Text = ""
        btnaddReg.Enabled = True
    End Sub

    Private Sub ReFreshListBox()
        Dim _help As New ShowHelper()
        '_help.ListBoxShowArray(lbRegList, regArray)
        _help.ListBoxShowHash(lbRegList, regHash)
        _help.ListBoxShowArray(lbUrlList, urlarray)
    End Sub

    '添加地址规则
    Private Sub btnAddUrlRule(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Button4.Enabled = False

        Dim _start As Integer = 0
        Dim _end As Integer = 0
        If txtStart.Text.Trim <> "" Then
            _start = CInt(txtStart.Text.Trim)
        End If

        If txtEnd.Text.Trim <> "" Then
            _end = CInt(txtEnd.Text.Trim)
        End If
        Dim _hp As New ShowHelper
        _hp.GetUrlList(txturl.Text.Trim, "*", _start, _end, urlarray)



        '将结果显示在文本框中
        '  _hp.TextBoxShowArray(txtUrlList, urlarray)
        _hp.ListBoxShowArray(lbUrlList, urlarray)

        ReFreshListBox()
        Button4.Enabled = True
    End Sub

    Private Sub lbUrlList_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lbUrlList.KeyDown
        'ReFreshListBox()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Button3.Enabled = False

        Dim fileurl As String = txtFileUrl.Text
        Dim namestring As String = txtNameUrl.Text
        fileurl = fileurl.Replace(vbCrLf, ",")
        namestring = namestring.Replace(vbCrLf, ",")
        Dim comm(1) As Char
        comm(1) = ","c 

        Dim urlArray As String() = fileurl.Split(comm, StringSplitOptions.RemoveEmptyEntries)
        Dim names As String() = namestring.Split(comm, StringSplitOptions.RemoveEmptyEntries)


        If urlArray.Length <> names.Length Then
            MessageBox.Show("两个长度不一样!")
            Return
        End If

        Dim taskhash As New Hashtable()
        Dim taskarray As New ArrayList()
        For i As Integer = 0 To urlArray.Length - 1
            Dim tpfd As New DownFileData
            tpfd.FileId = i
            tpfd.FileName = names(i)
            tpfd.FileUrl = urlArray(i)

            taskarray.Add(tpfd)




            'If Not taskhash.ContainsKey(tpfd.FileUrl) Then
            '    taskhash.Add(tpfd.FileUrl, tpfd)
            'End If

        Next

        '  Me.DownLoadFile(taskhash)
        Me.DownLoadFile(taskarray)
        MessageBox.Show("处理完成")

        Button3.Enabled = True


    End Sub

    Private Sub btnDownFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownFile.Click

        '下载所有的文件

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        '从文本框中粘贴地址
        ' urlarray
        Dim _hp As New ShowHelper
        Dim rf As New UrlFrm()
        rf.Tag = urlarray
        If rf.ShowDialog = Windows.Forms.DialogResult.OK Then
            _hp.ListBoxShowArray(lbUrlList, urlarray) 
            ReFreshListBox()

        End If


    End Sub

    Private picar As New ArrayList()
    Private Sub btn_save_web_pic_Click(sender As Object, e As EventArgs) Handles btn_save_web_pic.Click

        btn_save_web_pic.Enabled = False

        Dim sda As New OleDb.OleDbDataAdapter(txt_sql.Text.Trim, txt_connstring.Text.Trim)
        Dim dt As New DataTable
        sda.Fill(dt)

        Dim url As String
        Dim title As String
        Dim sb As New StringBuilder
        lb_pic_status.Text = "共"
        Dim i As Integer = 0
        For Each dr As DataRow In dt.Rows
            ' sb.Append(txt_title_field.Text.Trim & dr(txt_title_field.Text.Trim) & ":" & dr(txt_url_field.Text.Trim) & "\n")
            url = dr(txt_url_field.Text.Trim).ToString.Trim
            title = dr(txt_title_field.Text.Trim).ToString.Trim

            If url <> "" Then 

                Dim tp As New Web2Images
                tp.wurl = url
                tp.savename = title & ".png"
                tp.path = txt_save_path.Text.Trim

                ' tp.Show()
                tp.DoWork()
                Threading.Thread.Sleep(500)
                tp = Nothing

                Application.DoEvents()
                i += 1
                lb_pic_status.Text = i.ToString + "  共  " + dt.Rows.Count.ToString

            End If
        Next

        MsgBox("下载完成")
        btn_save_web_pic.Enabled = True



    End Sub
     

End Class





Public Class DownFileData
    Public FileUrl As String '文件地址
    Public FileName As String '文件名称
    Public FileId As String '文件的编号

End Class

''' <summary>
''' 对一个网页任务的描述
''' </summary>
''' <remarks></remarks>
Public Class RegexTask
    ''' <summary>
    ''' 下载地址
    ''' </summary>
    ''' <remarks></remarks>
    Public WebUrl As String
    ''' <summary>
    ''' 网页内容
    ''' </summary>
    ''' <remarks></remarks>
    Public WebString As String
End Class